home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / include / RCS / sync.h,v < prev    next >
Encoding:
Text File  |  1991-08-07  |  10.8 KB  |  402 lines

  1. head     1.7;
  2. branch   ;
  3. access   ;
  4. symbols  sprited:1.7.1;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.7
  10. date     90.01.16.17.59.49;  author jhh;  state Exp;
  11. branches 1.7.1.1;
  12. next     1.6;
  13.  
  14. 1.6
  15. date     89.10.25.12.14.22;  author shirriff;  state Exp;
  16. branches ;
  17. next     1.5;
  18.  
  19. 1.5
  20. date     89.07.31.17.33.13;  author jhh;  state Exp;
  21. branches ;
  22. next     1.4;
  23.  
  24. 1.4
  25. date     89.06.23.11.30.06;  author rab;  state Exp;
  26. branches ;
  27. next     1.3;
  28.  
  29. 1.3
  30. date     89.04.09.11.33.25;  author jhh;  state Exp;
  31. branches ;
  32. next     1.2;
  33.  
  34. 1.2
  35. date     89.01.06.11.38.27;  author jhh;  state Exp;
  36. branches ;
  37. next     1.1;
  38.  
  39. 1.1
  40. date     88.06.21.09.36.59;  author ouster;  state Exp;
  41. branches ;
  42. next     ;
  43.  
  44. 1.7.1.1
  45. date     91.08.07.11.27.30;  author kupfer;  state Exp;
  46. branches ;
  47. next     ;
  48.  
  49.  
  50. desc
  51. @@
  52.  
  53.  
  54. 1.7
  55. log
  56. @made lint happy
  57. @
  58. text
  59. @/*
  60.  * sync.h --
  61.  *
  62.  *     Typedefs and macros to support the use of monitors.
  63.  *      Clients use the macros to acquire and release monitor locks, and
  64.  *      to wait on and notify monitored condition variables.  Only Broadcast
  65.  *      semantics are supported for lock release; that is, all processes
  66.  *      waiting on a lock are made runnable when the locked is released.
  67.  *
  68.  *      These macros do a fast check on the state of the lock or condition
  69.  *      variable to reduce overhead.  In the best case the lock and unlock
  70.  *      routines just set or clear flags.  In the worst case these routines
  71.  *      have to call slower routines that must acquire a master lock.
  72.  *
  73.  * Copyright 1986, 1988 Regents of the University of California
  74.  * Permission to use, copy, modify, and distribute this
  75.  * software and its documentation for any purpose and without
  76.  * fee is hereby granted, provided that the above copyright
  77.  * notice appear in all copies.  The University of California
  78.  * makes no representations about the suitability of this
  79.  * software for any purpose.  It is provided "as is" without
  80.  * express or implied warranty.
  81.  *
  82.  *
  83.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.6 89/10/25 12:14:22 shirriff Exp Locker: jhh $ SPRITE (Berkeley)
  84.  */
  85.  
  86. #ifndef _SYNCUSER
  87. #define _SYNCUSER
  88.  
  89. #ifndef _SPRITE
  90. #include "sprite.h"
  91. #endif
  92.  
  93. /*
  94.  * The state of a lock consists of two bits of information, a lock bit
  95.  * and a waiting bit.  The lock bit indicates that someone has the
  96.  * lock, and no other process can acquire the lock until this bit is
  97.  * cleared.  The waiting bit indicates that someone wants the lock.
  98.  *
  99.  * The user locks are different from the kernel locks. If we are compiling
  100.  * user code then the Sync_UserLock type is typedef'ed to be Sync_Lock.
  101.  * The Sync_UserLock type is used by the kernel to differentiate between
  102.  * the two types of locks.
  103.  *
  104.  * Note: the inUse field is a whole word so it can be
  105.  *       an argument to the test-and-set instruction.
  106.  */
  107.  
  108. typedef struct Sync_UserLock {
  109.     Boolean inUse;        /* 1 while the lock is busy */
  110.     Boolean waiting;        /* 1 if someone wants the lock */
  111. } Sync_UserLock;
  112.  
  113. #ifndef KERNEL 
  114. typedef Sync_UserLock Sync_Lock;     /* define user locks */
  115. #endif
  116.  
  117. /*
  118.  * Condition variables represent events that are associated with
  119.  * locks.  The operations on a condition variable are Sync_Wait and
  120.  * Sync_Broadcast. The lock must be acquired before a call to
  121.  * Sync_Wait is made.  The lock is released while a process waits on a
  122.  * condition, and is then re-acquired when the condition is notified
  123.  * via Sync_Broadcast.
  124.  */
  125.  
  126. typedef struct Sync_Condition {
  127.     Boolean waiting;        /* 1 if someone is waiting on the condition */
  128. } Sync_Condition;
  129.  
  130. /*
  131.  * Maximum number of prior locks that can exist for any type of lock. A prior
  132.  * lock is the last lock to be grabbed by the process grabbing the current 
  133.  * lock. Keeping track of the locks held by a process is done in the proc
  134.  * module. By knowing all the prior locks of a lock it is possible to 
  135.  * construct a tree representing the locking behavior of the system.
  136.  * SYNC_MAX_PRIOR places a limit on how many prior locks will be remembered
  137.  * for an individual lock.
  138.  */
  139.  
  140. #ifndef LOCKDEP
  141. #define SYNC_MAX_PRIOR 1
  142. #else
  143. #define SYNC_MAX_PRIOR 30
  144. #endif
  145.  
  146. /*
  147.  * This structure is used to pass locking statistics to user programs.
  148.  * One of these structures is needed per lock type.
  149.  */
  150. typedef struct {
  151.     int        inUse;                /* 1 if structure is valid  */
  152.     int        class;                /* 0 = semaphore, 1 = lock */
  153.     int        hit;                /* # times lock was grabbed */
  154.     int        miss;                /* # times lock was missed */
  155.     char    name[30];            /* name of lock */
  156.     int        priorCount;            /* # of prior locks  */
  157.     int        priorTypes[SYNC_MAX_PRIOR];    /* types of prior locks */
  158.     int        activeCount;            /* # active locks of type */
  159.     int        deadCount;            /* # dead locks of type */
  160.     int        spinCount;            /* # spins waiting for lock */
  161. } Sync_LockStat;
  162.  
  163.  
  164. /*
  165.  * ----------------------------------------------------------------------------
  166.  *
  167.  * Monitor Locks --
  168.  *
  169.  *    The following set of macros are used to emulate monitored
  170.  *    procedures of Mesa.  The Sprite style document has a complete
  171.  *    description of the conventions required to emulate a set
  172.  *    of monitored procedures; a summary is presented here.
  173.  *
  174.  *      The LOCK_MONITOR and UNLOCK_MONITOR macros depend on a constant
  175.  *      LOCKPTR.  LOCKPTR should be defined as the address of the lock
  176.  *      variable used to lock the monitor.  Something like the following
  177.  *      two lines of code should appear at the beginning of a file of
  178.  *      monitored procedures.
  179.  *
  180.  *    Sync_Lock modMonitorLock;
  181.  *    #define LOCKPTR (&modMonitorLock)
  182.  *
  183.  *    The pseudo-keywords INTERNAL and ENTRY denote internal and entry
  184.  *    procedures of a monitor.  INTERNAL procedures can only be called
  185.  *    when the monitor lock is held.  ENTRY procedures are procedures
  186.  *    that acquire the lock.  There may also be External procedures.
  187.  *    They are the default and there is no special keyword.  An External
  188.  *    procedure doesn't explicitly acquire the monitor lock, but may
  189.  *    call an ENTRY procedure.
  190.  *
  191.  * ----------------------------------------------------------------------------
  192.  */
  193.  
  194. #define LOCK_MONITOR               (void) Sync_GetLock(LOCKPTR)
  195. #define UNLOCK_MONITOR             (void) Sync_Unlock(LOCKPTR)
  196.  
  197. #define ENTRY
  198. #define INTERNAL
  199.  
  200.  
  201. /*
  202.  * ----------------------------------------------------------------------------
  203.  *
  204.  * Sync_Wait --
  205.  *
  206.  * Wait on a condition variable.  This can only be called while a lock
  207.  * is aquired because it is only safe to check global state while in a
  208.  * monitor.  This releases the monitor lock and makes the process sleep
  209.  * on the condition. The monitor lock is reacquired after the process 
  210.  * has been woken up.
  211.  *
  212.  * The address of the condition variable is used as the generic 'event'
  213.  * that the process blocks on.  The value of the condition variable is
  214.  * used to indicate if there is a process waiting on the condition.
  215.  *
  216.  * Results:
  217.  *    None.
  218.  *
  219.  * Side effects:
  220.  *    Put the process to sleep and release the monitor lock.
  221.  *
  222.  * ----------------------------------------------------------------------------
  223.  */
  224.  
  225. #define Sync_Wait(conditionPtr, wakeIfSignal) \
  226.     Sync_SlowWait(conditionPtr, LOCKPTR, wakeIfSignal)
  227.  
  228.  
  229. /*
  230.  * ----------------------------------------------------------------------------
  231.  *
  232.  * Sync_Broadcast --
  233.  *
  234.  *      Notify other processes that a condition has been met.  If the
  235.  *      condition variable indicates that there are processes waiting
  236.  *      on the condition, then Sync_SlowBroadcast is used to wakeup
  237.  *      waiting processes.
  238.  *
  239.  * Monitor Lock:
  240.  *    This routine needs to be called with the monitor lock held.
  241.  *    This ensures synchronous access to the conditionPtr->waiting flag.
  242.  *
  243.  * Results:
  244.  *    None.
  245.  *
  246.  * Side effects:
  247.  *    Make the processes waiting on the condition variable runnable.
  248.  *
  249.  * ----------------------------------------------------------------------------
  250.  */
  251.  
  252. #define Sync_Broadcast(conditionPtr) \
  253.     if (((Sync_Condition *)conditionPtr)->waiting == TRUE)  { \
  254.     (void)Sync_SlowBroadcast((unsigned int) conditionPtr, \
  255.       &((Sync_Condition *)conditionPtr)->waiting); \
  256.     }
  257.  
  258. /*
  259.  * Exported procedures of the sync module for monitors.
  260.  */
  261.  
  262. extern ReturnStatus    Sync_GetLock();
  263. extern ReturnStatus    Sync_Unlock();
  264. extern ReturnStatus    Sync_SlowLock();
  265. extern ReturnStatus    Sync_SlowWait();
  266. extern ReturnStatus    Sync_SlowBroadcast();
  267.  
  268. #endif /* _SYNCUSER */
  269. @
  270.  
  271.  
  272. 1.7.1.1
  273. log
  274. @Branch for Sprite server changes.
  275. @
  276. text
  277. @d25 1
  278. a25 1
  279.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.7 90/01/16 17:59:49 jhh Exp $ SPRITE (Berkeley)
  280. @
  281.  
  282.  
  283. 1.6
  284. log
  285. @Put in (void) cast to fix lint problems.
  286. @
  287. text
  288. @d25 1
  289. a25 1
  290.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.5 89/07/31 17:33:13 jhh Exp Locker: shirriff $ SPRITE (Berkeley)
  291. d136 2
  292. a137 2
  293. #define LOCK_MONITOR               Sync_GetLock(LOCKPTR)
  294. #define UNLOCK_MONITOR             Sync_Unlock(LOCKPTR)
  295. @
  296.  
  297.  
  298. 1.5
  299. log
  300. @added spinCount to lock information
  301. @
  302. text
  303. @d25 1
  304. a25 1
  305.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.4 89/06/23 11:30:06 rab Exp Locker: jhh $ SPRITE (Berkeley)
  306. d196 1
  307. a196 1
  308.     Sync_SlowBroadcast((unsigned int) conditionPtr, \
  309. @
  310.  
  311.  
  312. 1.4
  313. log
  314. @*** empty log message ***
  315. @
  316. text
  317. @d25 1
  318. a25 1
  319.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.3 89/04/09 11:33:25 jhh Exp Locker: rab $ SPRITE (Berkeley)
  320. d102 1
  321. @
  322.  
  323.  
  324. 1.3
  325. log
  326. @moved locking stuff out of kernel/sync.h
  327. @
  328. text
  329. @d25 1
  330. a25 1
  331.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.2 89/01/06 11:38:27 jhh Exp Locker: jhh $ SPRITE (Berkeley)
  332. d209 1
  333. a209 1
  334. #endif _SYNCUSER
  335. @
  336.  
  337.  
  338. 1.2
  339. log
  340. @New Sync_Lock definition
  341. @
  342. text
  343. @d25 1
  344. a25 1
  345.  * $Header: /sprite/src/lib/include/RCS/sync.h,v 1.1 88/06/21 09:36:59 ouster Exp Locker: jhh $ SPRITE (Berkeley)
  346. d41 5
  347. d50 1
  348. a50 1
  349. typedef struct Sync_Lock_User {
  350. d53 1
  351. a53 1
  352. } Sync_Lock_User;
  353. d56 1
  354. a56 28
  355. typedef Sync_Lock_User Sync_Lock;     /* define user locks */
  356. #else
  357.  
  358. typedef struct Sync_Lock_Kernel {
  359.     Boolean inUse;            /* 1 while the lock is busy */
  360.     Boolean waiting;                /* 1 if someone wants the lock */
  361.     int hit;                /* number of times lock is grabbed */
  362.     int miss;                /* number of times lock is missed */
  363.     Address holderPC;            /* pc of lock holder */
  364.     Address holderPCBPtr;        /* process id of lock holder, this 
  365.                                          * should be a Proc_ControlBLock ptr
  366.                      * but including proc.h causes a 
  367.                      * circular include */
  368. } Sync_Lock_Kernel;
  369.  
  370. #ifdef CLEAN
  371. typedef Sync_Lock_User Sync_Lock;    /* define locks for a clean kernel */    
  372. #define SYNC_LOCK_INIT_STATIC() {0,0}
  373. #define SYNC_LOCK_INIT_DYNAMIC(lock) {(lock)->inUse = (lock)->waiting = 0;} 
  374. #else
  375. typedef Sync_Lock_Kernel Sync_Lock;    /* define locks for debuggin kernel */
  376. #define SYNC_LOCK_INIT_STATIC() {0,0,0,0,(Address) NIL, (Address) NIL}
  377. #define SYNC_LOCK_INIT_DYNAMIC(lock) { \
  378.     (lock)->inUse = (lock)->waiting = 0; \
  379.     (lock)->hit = (lock)->miss = 0; \
  380.     (lock)->holderPC = (Address) NIL; \
  381.     (lock)->holderPCBPtr  = (Address) NIL; }
  382. #endif
  383. d71 32
  384. @
  385.  
  386.  
  387. 1.1
  388. log
  389. @Initial revision
  390. @
  391. text
  392. @d25 1
  393. a25 1
  394.  * $Header: sync.h,v 1.1 88/06/19 14:34:05 ouster Exp $ SPRITE (Berkeley)
  395. d45 1
  396. a45 1
  397. typedef struct Sync_Lock {
  398. d48 32
  399. a79 1
  400. } Sync_Lock;
  401. @
  402.